﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace YArchitech.HVAC
{
	public partial class hyFrmManage : System.Windows.Forms.Form
	{
		public hyFrmManage(ExternalCommandData cmdData)
		{
			this.InitializeComponent();
			try
			{
				this.cmdDataForm = cmdData;
				XmlInfo.hyLoadXml();
			}
			catch
			{
			}
		}

		public hyFrmManage()
		{
			this.InitializeComponent();
		}

		private void SetGridView()
		{
			this.dataGridView1.Rows.Clear();
			this.dataGridView1.Columns.Clear();
			this.dataGridView1.RowHeadersVisible = false;
			this.dataGridView1.ColumnHeadersVisible = false;
			DataGridViewImageColumn dataGridViewImageColumn = new DataGridViewImageColumn();
			dataGridViewImageColumn.Name = "selLock";
			dataGridViewImageColumn.HeaderText = "锁";
			dataGridViewImageColumn.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewImageColumn);
			DataGridViewImageColumn dataGridViewImageColumn2 = new DataGridViewImageColumn();
			dataGridViewImageColumn2.Name = "selShow";
			dataGridViewImageColumn2.HeaderText = "显";
			dataGridViewImageColumn2.Width = 20;
			this.dataGridView1.Columns.Insert(0, dataGridViewImageColumn2);
			DataGridViewTextBoxColumn dataGridViewTextBoxColumn = new DataGridViewTextBoxColumn();
			dataGridViewTextBoxColumn.Name = "selName";
			dataGridViewTextBoxColumn.HeaderText = "选择集名称";
			dataGridViewTextBoxColumn.Width = 131;
			dataGridViewTextBoxColumn.ReadOnly = true;
			this.dataGridView1.Columns.Insert(0, dataGridViewTextBoxColumn);
		}

		private void BindData()
		{
			this.SetGridView();
			XmlNodeList xmlNodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet");
			if (xmlNodeList.Count > 0)
			{
				this.dataGridView1.Rows.Add(xmlNodeList.Count);
				for (int i = 0; i < xmlNodeList.Count; i++)
				{
					this.dataGridView1.Rows[i].Cells[0].Value = xmlNodeList.Item(i).Attributes["Text"].Value;
					if (xmlNodeList.Item(i).Attributes["Hide"].Value == "true")
					{
						this.dataGridView1.Rows[i].Cells[1].Value = this.imageList1.Images[0];
						this.dataGridView1.Rows[i].Cells[1].ToolTipText = "已显示";
					}
					else
					{
						this.dataGridView1.Rows[i].Cells[1].Value = this.imageList1.Images[1];
						this.dataGridView1.Rows[i].Cells[1].ToolTipText = "已隐藏";
					}
					if (xmlNodeList.Item(i).Attributes["Lock"].Value == "true")
					{
						this.dataGridView1.Rows[i].Cells[2].Value = this.imageList1.Images[2];
						this.dataGridView1.Rows[i].Cells[2].ToolTipText = "已锁定";
					}
					else
					{
						this.dataGridView1.Rows[i].Cells[2].Value = this.imageList1.Images[3];
						this.dataGridView1.Rows[i].Cells[2].ToolTipText = "已解锁";
					}
				}
			}
			if (xmlNodeList.Count < 11)
			{
				this.dataGridView1.Width = 175;
				this.dataGridView1.Height = xmlNodeList.Count * 23 + 3;
				base.Height = xmlNodeList.Count * 23 + 80 + 25;
			}
			else
			{
				this.dataGridView1.Width = 191;
				base.Width = 211;
				this.dataGridView1.Height = 233;
				base.Height = 335;
			}
			this.btnClose.Top = base.Height - 55;
		}

		private void hyFrmManage_Load(object sender, EventArgs e)
		{
			try
			{
				this.BindData();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			try
			{
				this.dataGridView1.Rows[e.RowIndex].Cells[0].Selected = true;
				XmlNodeList xmlNodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet");
				int columnIndex = e.ColumnIndex;
				if (e.ColumnIndex == 1)
				{
					if (xmlNodeList.Item(e.RowIndex).Attributes["Hide"].Value == "true")
					{
						this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.imageList1.Images[1];
						this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "已隐藏";
						xmlNodeList.Item(e.RowIndex).Attributes["Hide"].Value = "false";
						CmdTools.cdHide(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(), false);
					}
					else
					{
						this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.imageList1.Images[0];
						this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "已显示";
						xmlNodeList.Item(e.RowIndex).Attributes["Hide"].Value = "true";
						CmdTools.cdHide(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(), true);
					}
				}
				if (e.ColumnIndex == 2)
				{
					if (xmlNodeList.Item(e.RowIndex).Attributes["Lock"].Value == "true")
					{
						this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.imageList1.Images[3];
						this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "已解锁";
						xmlNodeList.Item(e.RowIndex).Attributes["Lock"].Value = "false";
						CmdTools.cdLock(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(), false);
					}
					else
					{
						this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = this.imageList1.Images[2];
						this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = "已锁定";
						xmlNodeList.Item(e.RowIndex).Attributes["Lock"].Value = "true";
						CmdTools.cdLock(this.cmdDataForm, this.dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(), true);
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			XmlNodeList xmlNodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet");
			if (e.ColumnIndex == 0)
			{
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;
				if ("False" == dataGridViewCheckBoxCell.Value.ToString())
				{
					xmlNodeList.Item(e.RowIndex).Attributes["Select"].Value = "false";
					return;
				}
				xmlNodeList.Item(e.RowIndex).Attributes["Select"].Value = "true";
			}
		}

		private void hyFrmManage_FormClosing(object sender, FormClosingEventArgs e)
		{
			WinFormTools.frmSelectSet = null;
			if (!WinFormTools.IsAdministrator())
			{
				MessageBox.Show("非管理员身份运行，数据无法保存。");
				return;
			}
			try
			{
				XmlInfo.hySaveXml();
				string text = "";
				ElementSet elements = new ElementSet();
				new cmdLoadSelSet().Execute(this.cmdDataForm, ref text, elements);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString());
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
				new hyFrmAdd().ShowDialog();
				this.BindData();
			}
			catch
			{
			}
		}

		public void TraverseGrid()
		{
			XmlNodeList xmlNodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet");
			foreach (object obj in xmlNodeList)
			{
				((XmlNode)obj).Attributes["Select"].Value = "false";
			}
			for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
			{
				if (this.dataGridView1.Rows[i].Cells[0].Selected)
				{
					xmlNodeList.Item(i).Attributes["Select"].Value = "true";
				}
			}
		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			try
			{
				this.TraverseGrid();
				XmlNodeList xmlNodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet[@Select='true']");
				if (xmlNodeList.Count == 0)
				{
					MessageBox.Show("请选择！");
				}
				else if (xmlNodeList.Count != 1)
				{
					MessageBox.Show("一次只能编辑一个！");
				}
				else
				{
					new hyFrmEdit().ShowDialog();
					this.BindData();
				}
			}
			catch
			{
			}
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
				this.TraverseGrid();
				XmlNodeList xmlNodeList = XmlInfo.hyXmlDoc.SelectNodes("SelSets/SelSet[@Select='true']");
				IEnumerator enumerator = xmlNodeList.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						if (WinFormTools.IsDefault(((XmlNode)enumerator.Current).Attributes["Text"].Value))
						{
							MessageBox.Show("默认选择集不可删除");
							return;
						}
					}
				}
				if (xmlNodeList.Count == 0)
				{
					MessageBox.Show("请选择！");
				}
				else if (MessageBox.Show("确定删除？", "盈建科提示", MessageBoxButtons.OKCancel) != DialogResult.Cancel)
				{
					foreach (object obj in xmlNodeList)
					{
						XmlNode xmlNode = (XmlNode)obj;
						xmlNode.ParentNode.RemoveChild(xmlNode);
					}
					this.BindData();
				}
			}
			catch (Exception ex)
			{
				WinFormTools.MsgBox(ex.ToString());
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private ExternalCommandData cmdDataForm;
	}
}
